package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;

	[SWF(width = 600, height = 600)]
	public class Game extends Sprite {
		private var _cellSize:int = 20;
		private var _grid:Grid;
		private var _player:Sprite;
		private var _index:int;
		private var _path:Array;

		public function Game() {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			makePlayer();
			makeGrid();
			stage.addEventListener(MouseEvent.CLICK, onGridClick);
		}

		/** 生成一个player角色(简单起见，就是一个圈) */
		private function makePlayer():void {
			_player = new Sprite();
			_player.graphics.beginFill(0xff0000);
			_player.graphics.drawCircle(0, 0, 5);
			_player.graphics.endFill();
			_player.x = Math.random() * 600;
			_player.y = Math.random() * 40;
			addChild(_player);
		}

		/** 生成网格，并随机放置一些障碍 */
		private function makeGrid():void {
			_grid = new Grid(30, 2);
			for (var i:int = 0; i < 200; i++) {
				_grid.setWalkable(Math.floor(Math.random() * 2), Math.floor(Math.random() * 2), false);
			}
			drawGrid();
		}

		/** 画网格线以及为障碍物填充颜色*/
		private function drawGrid():void {
			graphics.clear();
			for (var i:int = 0; i < _grid.numCols; i++) {
				for (var j:int = 0; j < _grid.numRows; j++) {
					var node:Node = _grid.getNode(i, j);
					graphics.lineStyle(0);
					graphics.beginFill(getColor(node));
					graphics.drawRect(i * _cellSize, j * _cellSize, _cellSize, _cellSize);
				}
			}
		}

		/** 返回节点颜色 */
		private function getColor(node:Node):uint {
			if (!node.walkable)
				return 0;
			if (node == _grid.startNode)
				return 0xcccccc;
			if (node == _grid.endNode)
				return 0xff0000;
			return 0xffffff;
		}

		/** 鼠标点击时随机设置终点，并以player当前位置做为起点 */
		private function onGridClick(event:MouseEvent):void {
			var xpos:int = Math.floor(mouseX / _cellSize);
			var ypos:int = Math.floor(mouseY / _cellSize);
			_grid.setEndNode(xpos, ypos);
			xpos = Math.floor(_player.x / _cellSize);
			ypos = Math.floor(_player.y / _cellSize);
			_grid.setStartNode(xpos, ypos);
			drawGrid();
			findPath();
		}

		/** 寻路 */
		private function findPath():void {
			var astar:AStar = new AStar();
			if (astar.findPath(_grid)) {
				_path = astar.path;
				_index = 0;
				addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}

		/**每帧的动画处理*/
		private function onEnterFrame(event:Event):void {
			var targetX:Number = _path[_index].x * _cellSize + _cellSize / 2;
			var targetY:Number = _path[_index].y * _cellSize + _cellSize / 2;

			//把经过的点，涂上黄色
			var passedNode:Node = _path[_index];
			graphics.lineStyle(0);
			graphics.beginFill(0xffff00);
			graphics.drawRect(passedNode.x * _cellSize, passedNode.y * _cellSize, _cellSize, _cellSize);

			var dx:Number = targetX - _player.x;
			var dy:Number = targetY - _player.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (dist < 1) {
				_index++; //索引加1，即取一个路径节点
				if (_index >= _path.length) //达到最后一个节点时，移除ENTER_FRAME监听
				{
					removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				}
			} else {
				_player.x += dx * .5;
				_player.y += dy * .5;
			}
		}
	}
}
